import os
from flask_migrate import Migrate
from app import create_app
from ext import db
from app.models import User, Post
import click

app = create_app(os.getenv('FLASK_CONFIG') or 'default')
# 传入2个对象一个是flask的app对象，一个是SQLAlchemy
migrate = Migrate(app, db)


@app.shell_context_processor
def make_shell_context():
    """
    :return:
    """
    return dict(app=app, db=db, User=User, Post=Post)


@app.cli.command()
@click.option('--drop', is_flag=True, help='Create after drop.')
def initdb(drop):
    """Init databases."""
    if drop:
        click.confirm(
            'This operation will delete the database, do you want to continue?',
            abort=True)
        db.drop_all()
        click.echo('Drop tables.')
    db.create_all()
    click.echo('Initialized database.')
